import 'package:flutter/material.dart';
import 'package:fsharing/core/viewmodels/pages/mine/manage/withdraw_account_model.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';

class WithdrawAccountPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => WithdrawAccountModel(Provider.of(context, listen: false)),
      child: BackgroundContainer(
        child: Scaffold(
          appBar: AppBar(
            leading: RoundBackButton(),
            title: Text('选择提现账户'),
          ),
          body: _ViewStateWidget(),
        ),
      ),
    );
  }
}

class _ViewStateWidget extends StatelessWidget {
  const _ViewStateWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    WithdrawAccountModel withdrawAccountModel = Provider.of(context);
    switch (withdrawAccountModel.viewState) {
      case ViewState.busy:
        return Center(
          child: CircularProgressIndicator(),
        );
      case ViewState.error:
        return Center(
          child: Text(withdrawAccountModel.errorMessage),
        );
      case ViewState.success:
        return _SuccessWidget(
          list: withdrawAccountModel.map.entries.toList(),
        );
      default:
        return Container();
    }
  }
}

class _SuccessWidget extends StatelessWidget {
  final List<MapEntry<String, dynamic>> list;

  const _SuccessWidget({
    Key key,
    @required this.list,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: list.length,
      itemBuilder: (context, index) {
        var mapEntry = list[index];
        return ListTile(
          title: Text(mapEntry.value),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () => _onItemClicked(context, mapEntry),
        );
      },
    );
  }

  void _onItemClicked(BuildContext context, MapEntry<String, dynamic> mapEntry) {
    Navigator.of(context).pop(mapEntry);
  }
}
